package com.tjetc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.tjetc.constant.CommonConstant;
import com.tjetc.context.BaseContext;
import com.tjetc.dao.*;
import com.tjetc.dao.impl.*;
import com.tjetc.exception.LoginException;
import com.tjetc.exception.MessageException;
import com.tjetc.exception.RegisterException;
import com.tjetc.pojo.dto.HistoryDTO;
import com.tjetc.pojo.dto.UserDTO;
import com.tjetc.pojo.entity.Admin;
import com.tjetc.pojo.entity.Booking;
import com.tjetc.pojo.entity.Order;
import com.tjetc.pojo.entity.User;
import com.tjetc.pojo.vo.GuestIdVO;
import com.tjetc.service.UserService;
import com.tjetc.utils.DruidUtils;

import java.util.List;

/**
 * @Author: itda
 * @Date: 2024/5/10 19:47
 * @Description: UserServiceImpl
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    private OrderDao orderDao = new OrderDaoImpl();

    private BookingDao bookingDao = new BookingDaoImpl();

    private GuestDao guestDao = new GuestDaoImpl();

    private RoomDao roomDao = new RoomDaoImpl();

    @Override
    public void login(UserDTO userDTO) {
        try {
            User user = userDao.getByUserName(userDTO.getUsername());
            if (user == null) {
                throw new LoginException(CommonConstant.USER_NAME_ERROR);
            }
            if (!user.getPassword().equals(userDTO.getPassword())) {
                throw new LoginException(CommonConstant.PASSWORD_ERROR);
            }
            BaseContext.setCurrentId(user.getId());
            System.out.println(user.getUsername() + CommonConstant.USER_LOGIN_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void register(UserDTO userDTO) {
        try {
            if (userDao.getByUserName(userDTO.getUsername()) != null) {
                throw new RegisterException(CommonConstant.USER_NAME_EXIST);
            }
            User user = new User();
            BeanUtil.copyProperties(userDTO, user);
            DruidUtils.start();
            userDao.insert(user);
            DruidUtils.commit();
            System.out.println(CommonConstant.REGISTER_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
    }

    @Override
    public void historicalOrders(HistoryDTO historyDTO) {
        try {
            GuestIdVO guestIdVO = guestDao.getByUserId(BaseContext.getCurrentId());
            if (guestIdVO == null) {
                throw new MessageException(CommonConstant.NO_FIND_DATA);
            }
            if (historyDTO.getOption() == 1) {
                List<Booking> bookings = bookingDao.getByGuestId(guestIdVO.getGuestId());
                if (bookings == null || bookings.size() == 0) {
                    throw new MessageException(CommonConstant.NO_FIND_DATA);
                }
                bookings.forEach(System.out::println);
            } else {
                List<Order> list = orderDao.getUserId(BaseContext.getCurrentId());
                if (list == null || list.size() == 0) {
                    throw new MessageException(CommonConstant.NO_FIND_DATA);
                }
                list.forEach(System.out::println);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void checkOutRoom() {
        try {
            if (guestDao.getByUserId(BaseContext.getCurrentId()) == null) {
                throw new MessageException(CommonConstant.NO_BOOKING);
            }
            List<Booking> bookings = bookingDao.getByGuestId(guestDao.getByUserId(BaseContext.getCurrentId()).getGuestId());
            if (bookings == null || bookings.size() == 0) {
                throw new MessageException(CommonConstant.NO_BOOKING);
            }
            Booking booking = bookings.get(0);
            DruidUtils.start();
            roomDao.updateStatusById(booking.getRoomId(), (short) 4);
            bookingDao.delete(booking.getId());
            DruidUtils.commit();
            System.out.println(CommonConstant.EXIT_BOOKING);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
    }
}
